from multiprocessing import Process, Lock
import time

class Response:
    status_code = 200


f = open("result.log", "a", encoding="utf-8")

def sim_request(index, lock):
    # 上下文管理, 自动管理获取和释放锁
    with lock:
        response = Response()
        time.sleep(0.2)
        print(f"index: {index}, current resonse: {response.status_code}")
        print(f"lock id: {id(lock)}")
        f.write(f"index: {index}, current resonse: {response.status_code}\n")


if __name__ == "__main__":
    p_array = []
    lock = Lock()
    for index in range(100):
        p = Process(target=sim_request, args=(index, lock))
        p.start()
        p_array.append(p)

    [p.join() for p in p_array]
    print("done!")